package com.testitems.Util;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ThreadLocalContext {
    public ThreadLocalContext() {}

    public static String jwtToken;

    private static ThreadLocal<Map<String, String>> properties = new ThreadLocal();

    public static String getJwtToken() {
        return getProperties("jwtToken");
    }

    public static void setJwtToken(String jwtToken) {
        setProperties("jwtToken",jwtToken);
    }

    public static void setProperties(String key, String value) {
        init();
        if (StringUtils.isNotBlank(value)) {
            ((Map)properties.get()).put(key,value);
        } else {
            ((Map)properties.get()).remove(key);
        }
    }

    public static String getProperties(String key) {
        init();
        return (String)((Map)properties.get()).get(key);
    }

    private static void init() {
        synchronized (Thread.currentThread()) {
            if (properties.get() == null) {
                Map<String, String> porpertiesMap = new ConcurrentHashMap<>();
                properties.set(porpertiesMap);
            }
        }
    }

}
